﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace AmazonAssociatesAPILinqtoXML
{
    /// <summary>
    /// Provides URLs to create XML files to parse
    /// </summary>
    public class AmazonURLClass
    {
        //Search Index values
        //http://docs.amazonwebservices.com/AWSEcommerceService/4-0/ApiReference/SearchIndexValues.html

        #region Public Properties



        private static string MY_AWS_ACCESS_KEY_ID
        {
            get
            {
                return ConfigurationManager.AppSettings["AwsAccessKeyId"];
            }
        }
        private static string MY_AWS_SECRET_KEY
        {
            get
            {
                return ConfigurationManager.AppSettings["AwsSecretKeyID"];
            }
        }
        private static string DESTINATION
        {
            get
            {
                return "ecs.amazonaws.com";
            }
        }

        /// <summary>
        /// Signs URL with datetime stamp and other security. Necessary to get XML file from Amazon
        /// </summary>
        private static SignedRequestHelper helper
        {
            get
            {
                return new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);
            }

        }

        #endregion

        /// <summary>
        /// Creates the URL for a searching books by keyword
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public string DoItemSearch(string keyword)
        {
            string requestString = "Service=AWSECommerceService"
            + "&Version=2009-03-31"
            + "&Operation=ItemSearch"
            + "&SearchIndex=Books"
            + "&ResponseGroup=Request,SalesRank,Small,Images,OfferSummary"
            + "&Keywords=" + keyword;
            string url = helper.Sign(requestString);
            return url;         
        }

        /// <summary>
        /// Creates the URL for searching for a book by ASIN, a product ID
        /// </summary>
        /// <param name="ItemId"></param>
        /// <returns></returns>
        public string DoItemSingleSearch(string ItemId)
        {
            string requestString = "Service=AWSECommerceService"
           + "&Version=2009-03-31"
           + "&Operation=ItemLookup"
           + "&ResponseGroup=Request,SalesRank,Small,Images,OfferSummary"
           + "&ItemId=" + ItemId;
            string url = helper.Sign(requestString);
            return url;
        }

        /// <summary>
        /// Creates URL for obtaining similar items, based on an Item ID
        /// </summary>
        /// <param name="ItemId"></param>
        /// <returns></returns>
        public string DoSimilarityItemLookUp(string ItemId)
        {
            string requestString = "Service=AWSECommerceService"
           + "&Version=2009-03-31"
           + "&Operation=SimilarityLookup"
           + "&SimilarityType=Random"
           + "&Condition=New"
           + "&ResponseGroup=Request,SalesRank,Small,Images,OfferSummary"
           + "&ItemId=" + ItemId;
            string url = helper.Sign(requestString);
            return url;
        }
    }
}
